﻿//(c) Copyright [2015] Hewlett-Packard Enterprise Development, L.P.
//All rights reserved.
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
//THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
//BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
//OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using HP.HPTRIM.Samples.Thumbnails;
using HP.HPTRIM.SDK;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MakeThumbnails
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();

        }

        private string _output;
        public string Output
        {
            get { return _output; }
            set
            {
                _output = value;
                RaisePropertyChanged("Output");
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            DesktopHelper dd = new DesktopHelper();
            Database database = dd.GetDatabase(new WindowInteropHelper(this).Handle);

            using (TrimMainObjectSearch recordSearch = new TrimMainObjectSearch(database, BaseObjectTypes.Record))
            {
                recordSearch.SetSearchString("recExtension:" + this.txtExtensions.Text);
                using (TrimMainObjectSearch newSearch = ObjectSelector.SelectMany(new WindowInteropHelper(this).Handle, recordSearch))
                {
                    if (newSearch != null)
                    {
                        if (MessageBox.Show("This will create a new thumbnail rendition for all selected Records, are you sure?", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                        {
                            foreach (Record record in newSearch)
                            {
                                if (_stopped)
                                {
                                    break;
                                }


                                try
                                {

                                    using (TempFile recordFile = new TempFile(record))
                                    {
                                        ThumbnailGenerator generator = new ThumbnailGenerator(recordFile.FilePath, record.SuggestedFileName);
                                        using (TempFile renditionFile = generator.MakeThumbnail())
                                        {
                                            record.SetThumbnailRendition(renditionFile);
                                        }
                                    }

                                    Output += string.Format("{0}: {1}\n", record.Number, record.Title);

                                }
                                catch(Exception ex) {
                                    Output += string.Format("Error processing {0}. {1}", record.Number, ex.Message);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        private bool _stopped = false;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            _stopped = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void txtOutput_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtOutput.ScrollToEnd();
        }


    }
}
